package com.huidao.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.huidao.entity.Role;
import com.huidao.entity.User;
import com.huidao.util.JDBCUtil;

public class UserDaoImpl implements UserDao {
	Connection conn = null;
	PreparedStatement ps = null;
	ResultSet rs = null;

	public boolean addUser(User user) {
		String sql = "INSERT INTO USER(USER_NAME,USER_PASSWORD,user_nickname,user_phone,user_truename,user_idnumber,ROLE_ID,user_shopname)VALUES(?,?,?,?,?,?,?,?)";
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1, user.getUserName());
			ps.setString(2, user.getUserPassword());
			ps.setString(3, user.getUserNickName());
			ps.setString(4, user.getUserPhone());
			ps.setString(5, user.getUserTrueName());
			ps.setString(6, user.getUserIdNumber());
			ps.setString(8, user.getUserShopName());
			if(user.getUserShopName()!=null){
				ps.setLong(7, 2);
			}else{
				ps.setLong(7, 1);
			}
			int i = ps.executeUpdate();
			if (i > 0) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.free(conn, ps, rs);
		}
		return false;
	}

	public boolean updateUser(User user) {
		String sql = "UPDATE User SET user_NAME=? ,user_password=?,user_nickname=?"
				+ ",user_phone=?,user_truename=?,user_idnumber=?,user_shopname=?"
				+ " WHERE user_ID=?";
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1, user.getUserName());
			ps.setString(2, user.getUserPassword());
			ps.setString(3, user.getUserNickName());
			ps.setString(4, user.getUserPhone());
			ps.setString(5, user.getUserTrueName());
			ps.setString(6, user.getUserIdNumber());
			ps.setLong(7, user.getRoleId().getRoleId());
			ps.setString(8, user.getUserShopName());
			int i = ps.executeUpdate();
			if (i > 0) {
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCUtil.free(conn, ps, rs);
		}
		return false;
	}

	public boolean isUserExist(User user) {
		String sql = "SELECT * FROM USER WHERE USER_NAME=?";
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1, user.getUserName());
			rs = ps.executeQuery();
			if (rs.next()) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.free(conn, ps, rs);
		}
		return false;
	}

	public boolean checkPassword(User user) {
		String sql = "SELECT * FROM USER WHERE USER_NAME=? AND USER_PASSWORD=?";
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1, user.getUserName());
			ps.setString(2, user.getUserPassword());
			rs = ps.executeQuery();
			if (rs.next()) {
				user.setUserId(rs.getLong("USER_ID"));
				user.setUserNickName(rs.getString("user_nickname"));
				user.setUserPhone(rs.getString("user_phone"));
				user.setUserTrueName(rs.getString("user_truename"));
				user.setUserIdNumber(rs.getString("user_idnumber"));
				user.setUserShopName(rs.getString("user_shopname"));
				Role role = new Role();
				role.setRoleId(rs.getLong("ROLE_ID"));
				user.setRoleId(role);
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.free(conn, ps, rs);
		}
		return false;
	}

	@Override
	public List<User> getListUser(String keyName, int currenpage, int pagesize,
			long uid) {
		List<User> list = new ArrayList<User>();
		String sql = "SELECT * FROM USER WHERE 1=1";
		if (keyName != null && !keyName.trim().equals("")) {
			sql = sql + "  AND USER_NAME LIKE '%" + keyName + "%'";
		}
		if (uid != 0) {
			sql = sql + " AND ROLE_ID = ' " + uid + " ' ";
		}
		sql = sql + " LIMIT ?,?";
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, (currenpage - 1) * pagesize);
			ps.setInt(2, pagesize);
			rs = ps.executeQuery();
			while (rs.next()) {
				User user = new User();
				user.setUserShopName(rs.getString("USER_SHOPNAME"));
				user.setUserName(rs.getString("USER_NAME"));
				list.add(user);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCUtil.free(conn, ps, rs);
		}
		return list;
	}

	@Override
	public int total(String keyName, long uid) {
		String sql = "SELECT COUNT(*) FROM USER WHERE 1=1";
		if (keyName != null && !keyName.trim().equals("")) {
			sql = sql + "  AND goods_name LIKE '%" + keyName + "%'";
		}
		if (uid != 0) {
			sql = sql + "  AND goods_id = ' " + uid + " ' ";
		}
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			if (rs.next()) {
				return rs.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCUtil.free(conn, ps, rs);
		}
		return 0;
	}

	@Override
	public int getTotalCount(String namekey, Integer roleId) {
		// TODO Auto-generated method stub
		String sql = "SELECT COUNT(*) FROM USER WHERE 1=1 ";
		if (namekey != null && !namekey.trim().equals("")) {
			sql = sql + " AND USER_NAME LIKE '%" + namekey + "%' ";
		}
		if (roleId != null && roleId != 0) {
			sql = sql + " AND ROLE_ID=" + roleId;
		}
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			if (rs.next()) {
				return rs.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCUtil.free(conn, ps, rs);
		}
		return 0;
	}

	@Override
	public boolean delUser(Long id) {
		// TODO Auto-generated method stub
		String sql = "DELETE FROM USER WHERE USER_ID=?";

		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setLong(1, id);
			int i = ps.executeUpdate();
			if (i > 0) {
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCUtil.free(conn, ps, rs);
		}
		return false;
	}

}
